package me.jzn.alib.utils;

import android.Manifest.permission;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.os.Build;
import android.telephony.TelephonyManager;

import androidx.annotation.RequiresPermission;
import androidx.annotation.WorkerThread;

import java.net.InetAddress;
import java.net.UnknownHostException;

import me.jzn.alib.enumres.NetType;
import me.jzn.core.utils.CommUtil;

/**
 * @author xqs @date 2019/7/20
 */
public class NetUtil {
    /**
     * 使用前确保domainNmae是域名，可简单判断后两位是否包含数字，因为顶级域名后两位肯定不是数字.
     */
    @WorkerThread
    public static String getIpByDomainName(String domainName) {
        try {
            InetAddress address = InetAddress.getByName(domainName);
            String ip = address.getHostAddress();
            if (!CommUtil.isEmpty(ip)) {
                return ip;
            }
            return null;
        } catch (UnknownHostException e) {
            return null;
        }
    }

    @RequiresPermission(permission.ACCESS_NETWORK_STATE)
    public static boolean isAvailable() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            ConnectivityManager cm = CtxUtil.getSystemService(ConnectivityManager.class);
            Network network = cm.getActiveNetwork();
            NetworkCapabilities networkCapabilities = cm.getNetworkCapabilities(network);
            if (networkCapabilities != null && networkCapabilities
                    .hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
                return true;
            }
            return false;
        }
        NetworkInfo info = getActiveNetworkInfo();
        if (info != null) {
            return isActive(info);
        }
        return false;
    }

    @RequiresPermission(permission.ACCESS_NETWORK_STATE)
    public static final NetworkInfo getActiveNetworkInfo() {
        ConnectivityManager cm = CtxUtil.getSystemService(ConnectivityManager.class);
        return cm.getActiveNetworkInfo();
    }

    @RequiresPermission(permission.ACCESS_NETWORK_STATE)
    public static final boolean isActive(NetworkInfo networkInfo) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            ConnectivityManager cm = CtxUtil.getSystemService(ConnectivityManager.class);
            Network network = cm.getActiveNetwork();
            NetworkCapabilities networkCapabilities = cm.getNetworkCapabilities(network);
            if (networkCapabilities != null && networkCapabilities
                    .hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
                return true;
            }
            return false;
        }
        return networkInfo.isAvailable() && networkInfo.isConnected();
    }

    @RequiresPermission(permission.ACCESS_NETWORK_STATE)
    @NetType
    public static int getNetworkType(NetworkInfo networkInfo) {
        if (isActive(networkInfo)) {
            return NetType.NONE;
        }
        if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
            return NetType.WIFI;
        }
        if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
            String _strSubTypeName = networkInfo.getSubtypeName();
            int subtype = networkInfo.getSubtype();
            switch (subtype) {
                case TelephonyManager.NETWORK_TYPE_GPRS:
                case TelephonyManager.NETWORK_TYPE_GSM:
                case TelephonyManager.NETWORK_TYPE_EDGE:
                case TelephonyManager.NETWORK_TYPE_CDMA:
                case TelephonyManager.NETWORK_TYPE_1xRTT:
                case TelephonyManager.NETWORK_TYPE_IDEN:
                    return NetType.G2;
                case TelephonyManager.NETWORK_TYPE_UMTS:
                case TelephonyManager.NETWORK_TYPE_EVDO_0:
                case TelephonyManager.NETWORK_TYPE_EVDO_A:
                case TelephonyManager.NETWORK_TYPE_HSDPA:
                case TelephonyManager.NETWORK_TYPE_HSUPA:
                case TelephonyManager.NETWORK_TYPE_HSPA:
                case TelephonyManager.NETWORK_TYPE_EVDO_B:
                case TelephonyManager.NETWORK_TYPE_EHRPD:
                case TelephonyManager.NETWORK_TYPE_HSPAP:
                case TelephonyManager.NETWORK_TYPE_TD_SCDMA:
                    return NetType.G3;
                case TelephonyManager.NETWORK_TYPE_LTE:
                case TelephonyManager.NETWORK_TYPE_IWLAN:
                    //TelephonyManager.NETWORK_TYPE_LTE_CA:
                case 19:
                    return NetType.G4;
                default:
                    return NetType.NONE;
            }
        }
        return NetType.NONE;
    }
}
